<?php
class LoginAction extends BaseAction{
	protected function _initialize() {
		$this->init();
	}
   /*
    +----------------------------------------------------------
    * 登录界面显示
    +----------------------------------------------------------
    */
    public function index()
    {
        $this->display();	
    }
	
	/*
    +----------------------------------------------------------
    * 登录验证
    +----------------------------------------------------------
    */
	public function logins(){
		if ($_SERVER["REQUEST_METHOD"] != "POST")
			return false;
		$admin = $_POST['admin'];
		$password = $_POST['password'];
		if (empty($admin))
			$this->error("请填写用户名");
		if (empty($password))
			$this->error("请填写密码");
		$User = D('Admin');
		$user = $User->where("admin_name='$admin'")->find();
		if($user){
			if(md5($password)!=$user['password']){
				$this->error('密码有误!');
			}else{
				Session::set("admin_id", $user["admin_id"]);
				Session::set("admin_name", $user["admin_name"]);
				$User->where("admin_name='$admin'")->setField('last_time',time());
				//添加action表信息
				$data['admin_id'] = $user["admin_id"];
				$data['login_time'] = time();
				$data['logout_time'] = 0;
				$data['name'] = $user["admin_name"];
				if(1 == $user['power']){
					$data['level'] = '超级管理员';
				}else{
					$data['level'] = '普通管理员';
				}
				D('Action')->add($data);
				redirect(__APP__);
			}
		}else{
			$this->error('用户名不存在!');
		}
	}
	
	/*
    +----------------------------------------------------------
    * 用户注销
    +----------------------------------------------------------
    */
	public function logout() {
		$aid = Session::get('admin_id');
		if ($aid) {
			Session::clear();
			//保存管理员注销时间到action表
			$data['action_id'] = $this->action_id;
			$data['logout_time'] = time();
			$result = D('Action')->save($data);
			$this->success('注销成功！',__APP__);
			
		} else {
			$this->error('已经注销！');	
		}
	}
	
	
}
?>
